<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*报表*/
class RePortForm extends AdminBase{
	protected $beforeActionList=[
		//'before_index'=>['onli'=>'index'],
	];
	function _initialize(){
		parent::_initialize();
	}
	/*报表*/
	function index(){
		$this->_index();
		echo $this->fetch();
	}
	/**
	 * 获取总统计数据
	 * http://smartgym.com/admin/reportForm/getData
	 * */
	function getData(){
		db()->execute("call reportform(@brandcount,@shopcount,@crashsuccesscount,@crasherrorcount,@membercount,@staffcount,@tempcount,@logincount)");
		$result=db()->query("select @brandcount as brandcount,@shopcount as shopcount,@crashsuccesscount as crashsuccesscount,@crasherrorcount as crasherrorcount,@membercount as membercount,@staffcount as staffcount,@tempcount as tempcount,@logincount as logincount");
		return  $this->ajax_success("成功",$result[0]);
	}
	
	/**
	 * 获取健身房相关统计
	 * http://smartgym.com/admin/reportForm/getData1
	 * */
	function _index(){
		if(empty(cache("ReportForm"))){
			//每个店铺会员统计
		   $list=	db("Interface")->where("is_original=1")->group("shop_id")->order("sum(owner_type=1) desc")->column("shop_id,sum(owner_type=1) as membercount,sum(owner_type=2) as staffcount");
		   //每个店铺刷卡统计
		   $logs=db("BehaviorLog")->group("shop_id")->column("shop_id,sum(owner_type=1) as m_in_count,sum(owner_type=2) as s_in_count,sum(owner_type=3) as t_in_count");
		   //每个店铺登录统计
		   $logins=db("AdminLog")->group("shop_id")->column("shop_id,count(1)");
		   //店铺
		   $shops=db("Shop")->where("parent_id!=0")->column("id,shop_name");
		   foreach ($list as $key => $value) {
		   	  if(isset($logs[$key])){
		   	  	  $list[$key]['m_in_count']=$logs[$key]['m_in_count'];
				  $list[$key]['s_in_count']=$logs[$key]['s_in_count'];
				  $list[$key]['t_in_count']=$logs[$key]['t_in_count'];
		   	  }else{
		   	  		$list[$key]['m_in_count']=0;
				  $list[$key]['s_in_count']=0;
				  $list[$key]['t_in_count']=0;
		   	  }
			  if(isset($logins[$key])){
		   	  	  $list[$key]['logincount']=$logins[$key];
		   	  }else{
		   	  	$list[$key]['logincount']=0;
		   	  }
			   
			  if(isset($shops[$key])){
		   	  	  $list[$key]['shop_name']=$shops[$key];
		   	  } else{
		   	  	  $list[$key]['shop_name']=0;
		   	  }
		   }
		     cache("ReportForm",$list,3600);
	  		 $today_list=$this->today_crash_statistic();
	  		 cache("TodayReportForm",$today_list,3600);
		}
		
	   $this->assign("list",cache("ReportForm"));
	   $this->assign("today_list",cache("TodayReportForm"));
	}
	/*今日刷卡统计*/
	function today_crash_statistic(){
		//每个店铺会员统计
	   $list=	db("Interface")->where("is_original=1")->group("shop_id")->order("count(1) desc")->column("shop_id,count(1) as total_count,owner_type");
	   //每个店铺刷卡统计
	   $logs=db("BehaviorLog")->where("date_format(create_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d') ")->group("shop_id")->column("shop_id,sum(owner_type=1) as m_in_count,sum(owner_type=2) as s_in_count,sum(owner_type=3) as t_in_count");
	   //每个店铺登录统计
	   $logins=db("AdminLog")->where("date_format(create_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d') ")->group("shop_id")->column("shop_id,count(1)");
	   //店铺
	   $shops=db("Shop")->where("parent_id!=0")->column("id,shop_name");
	   foreach ($list as $key => $value) {
	   	  if(isset($logs[$key])){
	   	  	  $list[$key]['today_m_in_count']=$logs[$key]['m_in_count'];
			  $list[$key]['today_s_in_count']=$logs[$key]['s_in_count'];
			  $list[$key]['today_t_in_count']=$logs[$key]['t_in_count'];
	   	  }else{
	   	  		$list[$key]['today_m_in_count']=0;
			   $list[$key]['today_s_in_count']=0;
			  $list[$key]['today_t_in_count']=0;
	   	  }
		  if(isset($logins[$key])){
	   	  	  $list[$key]['today_logincount']=$logins[$key];
	   	  }else{
	   	  	$list[$key]['today_logincount']=0;
	   	  }
		   
		  if(isset($shops[$key])){
	   	  	  $list[$key]['shop_name']=$shops[$key];
	   	  } else{
	   	  	  $list[$key]['shop_name']=0;
	   	  }
	   }
	   return $list;
	}
}
